package com.xnyzc.lhy.order.service.impl.order;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.order.entity.param.order.OrderParam;
import com.xnyzc.lhy.order.entity.order.OaCOrderPersonal;
import com.xnyzc.lhy.order.entity.gps.OaDVehicleGps;
import com.xnyzc.lhy.order.mapper.order.OaCOrderPersonalMapper;
import com.xnyzc.lhy.order.service.order.IOaCOrderPersonalService;
import com.xnyzc.lhy.order.service.order.IOaDOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 自费叫车订单 服务实现类
 * </p>
 *
 * @author lhy@xnyzc.com
 * @since 2019-07-22
 */
@Service
public class OaCOrderPersonalServiceImpl extends ServiceImpl<OaCOrderPersonalMapper, OaCOrderPersonal> implements IOaCOrderPersonalService {

    @Autowired
    private OaCOrderPersonalMapper personalMapper;
    @Autowired
    private IOaDOrderService oaDOrderService;

    @Override
    public boolean distributePersonOrder(OaCOrderPersonal oaCOrderPersonal, OrderParam oaDOrder, OaDVehicleGps nearCar) {
        try {
            Long personalId = oaCOrderPersonal.getPersonalId();
            oaDOrderService.addOrder(nearCar, oaDOrder);
            oaCOrderPersonal.setVehicleType(nearCar.getVehType());
            oaCOrderPersonal.setDriverId(nearCar.getDriverId());
            return this.updateById(oaCOrderPersonal);
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public String orderPerson(OaCOrderPersonal oaCOrderPersonal, OrderParam oaDOrder) {
        long id = IDUtil.nextId();
        //生成自费订单
        oaCOrderPersonal.setIsCarpool(oaDOrder.getIsCarpool());
        oaCOrderPersonal.setCarpoolNumber(oaDOrder.getCarpoolNumber());
        oaCOrderPersonal.setPersonalId(id);
        //是拼单产生流水号
        oaCOrderPersonal.setCarpoolSerialNo(oaDOrder.getCarpoolSerialNo());
        oaCOrderPersonal.setOrderNo(oaDOrder.getOrderNo());
        if (!this.save(oaCOrderPersonal)) {
            throw PangException.create(EErrorCode.noData);
        }
        return String.valueOf(id);
    }

}
